<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formApiTrait{
    public function index_api(Content $content)
    {
        return $content->title('API配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_api()->render()}</div>
HTML
            );
    }
    protected function form_api(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->tab('物流', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">说明：<br><a href="https://market.aliyun.com/products/57126001/cmapi021863.html?spm=5176.730005.productlist.d_cmapi021863.ebSZX2#sku=yuncode1586300000" target="_blank">点击进入</a>，购买后，等大概30s，在【已购买服务】里找到【全国快递物流查询-快递查询接口】，复制AppCode填入后台</div>';
            }, '');
            $form->text('app_ali_wuliu_appcode', '阿里全国快递物流查询AppCode')->default(Settings::get('app_ali_wuliu_appcode', ''));
        });
        $form->tab('内容审查', function (Form $form) {
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">说明：1: 你创建一个应用后，还需要领取对应的资源包，选择“内容审核平台-文本”，<a href="https://console.bce.baidu.com/ai/?fromai=1#/ai/antiporn/overview/resource/getFree" target="_blank">领取地址>></a><br/></div>';
            }, '');
            $res4 = __system__paycode__tip__(4);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res4['tip']);
            $form->text('app_baidu_filter_api_key', '百度内容审查API Key')->help($res4['text'].'申请地址：<a href="https://console.bce.baidu.com/ai/#/ai/antiporn/app/list" target="_blank">https://console.bce.baidu.com/ai/#/ai/antiporn/app/list</a>')->default(Settings::get('app_baidu_filter_api_key', ''));
            $form->text('app_baidu_filter_secret_key', '百度内容审查Secret Key')->default(Settings::get('app_baidu_filter_secret_key', ''));
            $form->text('app_baidu_filter_strategy_id', '百度内容审查-策略id')->help('默认有一个策略1，如果你新建了自己的策略，可以在<a href="https://ai.baidu.com/censoring#/strategylist">策略列表</a>查看id  ，<a href="https://img.mini.minisns.cn/images/bg/baidu_straget_tip.jpg" target="_blank">请看教程</a>')->default(Settings::get('app_baidu_filter_strategy_id', 1));
        });
        $form->tab('ip归属地', function (Form $form) {
            $form->text('app_ali_ip_address_appcode', '阿里全球IP归属地查询AppCode')->help('说明：<a href="https://market.aliyun.com/products/57002002/cmapi00035184.html?spm=5176.2020520132.101.3.2f8f7218mWulXN#sku=yuncode2918400001" target="_blank">点击进入</a>，购买后，在【已购买服务】里找到【全球IP归属地查询】，复制AppCode填入后台')->default(Settings::get('app_ali_ip_address_appcode', ''));
        });
        $form->tab('实名认证', function (Form $form) {
            $form->text('app_ali_real_name_appcode', '【数脉API】手机三要素')->help('说明：<a href="https://market.aliyun.com/products/57000002/cmapi026100.html?spm=5176.2020520132.101.3.72fd72186kDT9w#sku=yuncode20100000022" target="_blank">点击进入</a>，购买后，在【已购买服务】里找到【数脉API】手机三要素，复制AppCode填入后台')->placeholder('e380d9c5f6bc44338487476c281dd0ba')->default(Settings::get('app_ali_real_name_appcode', ''));
        });
        $form->tab('短信', function (Form $form) {
            $form->radio('app_sms_platform', '短信平台')->options([0=>'腾讯云', 1=>'阿里云', 2=>'短信宝'])->when(0, function (Form $form){
                $form->html(function () {
                    return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">说明：默认和cos是同一个腾讯云账号，如果不是，则在下面配置secret-key等两项</div>';
                }, '');
                $form->html(function () {
                    return '<div style="color: #414750;padding: 10px;border-radius: 5px;">短信参考模板：<span style="color: red">你的验证码为：{1}，若非本人操作，请勿泄露。</span></div>';
                }, '');
                $form->text('app_sms_tecent_appid', '腾讯短信SDK AppID')->placeholder('1400730235')->help('查看位置：腾讯短信后台-应用管理-应用列表-系统默认应用-SDK AppID')->default(Settings::get('app_sms_tecent_appid', ''));
                $form->text('app_sms_tecent_appkey', '腾讯短信App Key')->placeholder('4bb85a61119737f0c18cc222513a8743')->help('查看位置：腾讯短信后台-应用管理-应用列表-系统默认应用-App Key')->default(Settings::get('app_sms_tecent_appkey', ''));
                $form->text('app_sms_tecent_sign_content', '腾讯国内短信签名')->placeholder('宠也网')->help('注意：非签名ID 查看位置：腾讯短信后台-国内短信-签名管理-内容')->default(Settings::get('app_sms_tecent_sign_content', ''));
                $form->text('app_sms_tecent_template_id', '腾讯国内短信模版ID')->placeholder('1297874')->help('查看位置：腾讯短信后台-国内短信-正文模版管理-ID')->default(Settings::get('app_sms_tecent_template_id', ''));
                $form->divider();
                $form->text('app_sms_tecent_out_sign_content', '腾讯国际短信签名')->placeholder('宠也网')->help('注意：非签名ID 查看位置：腾讯短信后台-国际短信-签名管理-内容')->default(Settings::get('app_sms_tecent_out_sign_content', ''));
                $form->text('app_sms_tecent_out_template_id', '腾讯国际短信模版ID')->placeholder('1297874')->help('查看位置：腾讯短信后台-国际短信-正文模版管理-ID')->default(Settings::get('app_sms_tecent_out_template_id', ''));
                $form->html(function () {
                    return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">如果短信与cos不在同一个账号，需要配置下面</div>';
                }, '');
                $form->text('app_sms_tecent_secretId', 'SecretId')->placeholder('AKIDSr30jEUUFl23pNl25uUpvlMipgIgppgK')->default(Settings::get('app_sms_tecent_secretId', ''));
                $form->text('app_sms_tecent_secretKey', 'SecretKey')->placeholder('e9SWN1Um05yd4EAbKbdS3QYR5UeZIrEO')->default(Settings::get('app_sms_tecent_secretKey', ''));
            })->when(1, function (Form $form){
                $form->html(function () {
                    return '<div style="color: #414750;padding: 10px;border-radius: 5px;">短信参考模板：<span style="color: red">你的验证码为：{code}，若非本人操作，请勿泄露。</span></div>';
                }, '');
                $form->text('app_sms_aliyun_secretId', 'SecretId')->placeholder('AKIDSr30jEUUFl23pNl25uUpvlMipgIgppgK')->default(Settings::get('app_sms_aliyun_secretId', ''));
                $form->text('app_sms_aliyun_secretKey', 'SecretKey')->placeholder('e9SWN1Um05yd4EAbKbdS3QYR5UeZIrEO')->default(Settings::get('app_sms_aliyun_secretKey', ''));

                $form->text('app_sms_aliyun_sign_name', '短信签名名称（不是模版名称！）')->placeholder('宠也')->default(Settings::get('app_sms_aliyun_sign_name', ''));
                $form->text('app_sms_aliyun_template_id', '国内短信模版ID')->placeholder('SMS_44220021')->default(Settings::get('app_sms_aliyun_template_id', ''));
                $form->text('app_sms_aliyun_out_template_id', '国际短信模版ID')->placeholder('SMS_211488031')->default(Settings::get('app_sms_aliyun_out_template_id', ''));
            })->when(2, function (Form $form){
                $form->text('app_sms_smsbao_username', '用户名')->help('<a href="https://www.smsbao.com/" target="_blank">官网：https://www.smsbao.com/</a>>')->default(Settings::get('app_sms_smsbao_username', ''));
                $form->text('app_sms_smsbao_password', '密码')->default(Settings::get('app_sms_smsbao_password', ''));
                $form->text('app_sms_smsbao_sign', '签名')->help('短信宝签名不用审核，你填自己app名称即可')->default(Settings::get('app_sms_smsbao_sign', Settings::get('app_title', '宠也')));
            })->default(Settings::get('app_sms_platform', 0));


        });
        $form->tab('邮件', function (Form $form) {
            $form->radio('app_email_mailer', '协议')->options(['smtp'=>'SMTP'])->default(Settings::get('app_email_mailer', 'smtp'));
            $form->text('app_email_host', '服务器地址')->placeholder('smtp.163.com')->default(Settings::get('app_email_host', ''));
            $form->radio('app_email_encryption', '加密方式')->options(['ssl'=>'ssl'])->default(Settings::get('app_email_encryption', 'ssl'));
            $form->text('app_email_port', '端口')->placeholder('465')->default(Settings::get('app_email_port', '465'));
            $form->email('app_email_user_name', '发送人地址')->placeholder('wengeshuzi@163.com')->default(Settings::get('app_email_user_name', ''));
            $form->password('app_email_password', '密码')->help('<a href="https://img.mini.minisns.cn/hbx/smtp.png" target="_blank">查看配置教程</a>')->default(Settings::get('app_email_password', ''));
        });
        $form->tab('高德开放平台', function (Form $form) {
            $form->text('app_gaode_web_server_key', 'Web服务key')->help('有些定位服务需要用到, 注意一下高德web服务有两种，创建时不要选js api，点击进入<a href="https://console.amap.com/dev/key/app" target="_blank">高德开放平台</a>，最终<a href="https://img.mini.minisns.cn/hbx/map_location.jpg" target="_blank">示例</a>')->placeholder('05eddf51e6a7a9352982589475d212c9')->default(Settings::get('app_gaode_web_server_key', ''));
        });

        $form->tab('uni-push 1.0', function (Form $form) {

            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">在Dcloud后台申请uni-push 1.0，<a href="https://dev.dcloud.net.cn/pages/app/push/index" target="_blank">点击进入</a>，所需配置的参数<a href="https://img.mini.minisns.cn/hbx/push_tip.jpg" target="_blank">请看这里</a></div>';
            }, '');

            $form->text('uni_push_1_appid', 'AppId')->placeholder('ZTuYZHOmiWAbTeGZbyhULA')->default(Settings::get('uni_push_1_appid', ''));
            $form->text('uni_push_1_appkey', 'AppKey')->placeholder('6146f50410aac3e81862c8b62a4e952')->default(Settings::get('uni_push_1_appkey', ''));
            $form->text('uni_push_1_master_secret', 'MasterSecret')->placeholder('hRBxIf2mirAUNEA7cKeml1')->default(Settings::get('uni_push_1_master_secret', ''));

        });

        $form->action('settings/action/api');
        return $form;
    }

    public function api(Request $request){
        SiteUtils::forget_config_data();

        _update_env([
            "MAIL_MAILER" => $request->input('app_email_mailer', 'smtp'),
            "MAIL_HOST" => $request->input('app_email_host'),
            "MAIL_PORT" => $request->input('app_email_port', '465'),
            "MAIL_USERNAME" => $request->input('app_email_user_name', ''),
            "MAIL_PASSWORD" => $request->input('app_email_password', ''),
            "MAIL_ENCRYPTION" => $request->input('MAIL_ENCRYPTION', 'ssl'),
            "MAIL_FROM_ADDRESS" => $request->input('app_email_user_name', ''),
        ]);

        Settings::set('app_ali_wuliu_appcode', $request->input('app_ali_wuliu_appcode'), true);
        Settings::set('app_baidu_filter_api_key', $request->input('app_baidu_filter_api_key'), true);
        Settings::set('app_baidu_filter_secret_key', $request->input('app_baidu_filter_secret_key'), true);
        Settings::set('app_baidu_filter_strategy_id', $request->input('app_baidu_filter_strategy_id'), true);
        Settings::set('app_ali_ip_address_appcode', $request->input('app_ali_ip_address_appcode'), true);
        Settings::set('app_ali_real_name_appcode', $request->input('app_ali_real_name_appcode'), true);

        Settings::set('app_email_mailer', $request->input('app_email_mailer'), true);
        Settings::set('app_email_host', $request->input('app_email_host'), true);
        Settings::set('app_email_encryption', $request->input('app_email_encryption'), true);
        Settings::set('app_email_port', $request->input('app_email_port'), true);
        Settings::set('app_email_user_name', $request->input('app_email_user_name'), true);
        Settings::set('app_email_password', $request->input('app_email_password'), true);

        Settings::set('app_sms_platform', $request->input('app_sms_platform'), true);
        Settings::set('app_sms_tecent_appid', $request->input('app_sms_tecent_appid'), true);
        Settings::set('app_sms_tecent_appkey', $request->input('app_sms_tecent_appkey'), true);
        Settings::set('app_sms_tecent_sign_content', $request->input('app_sms_tecent_sign_content'), true);
        Settings::set('app_sms_tecent_template_id', $request->input('app_sms_tecent_template_id'), true);

        Settings::set('app_sms_tecent_out_sign_content', $request->input('app_sms_tecent_out_sign_content'), true);
        Settings::set('app_sms_tecent_out_template_id', $request->input('app_sms_tecent_out_template_id'), true);

        Settings::set('app_sms_aliyun_secretId', $request->input('app_sms_aliyun_secretId'), true);
        Settings::set('app_sms_aliyun_secretKey', $request->input('app_sms_aliyun_secretKey'), true);
        Settings::set('app_sms_aliyun_sign_name', $request->input('app_sms_aliyun_sign_name'), true);
        Settings::set('app_sms_aliyun_template_id', $request->input('app_sms_aliyun_template_id'), true);
        Settings::set('app_sms_aliyun_out_template_id', $request->input('app_sms_aliyun_out_template_id'), true);

        Settings::set('app_sms_tecent_secretId', $request->input('app_sms_tecent_secretId'), true);
        Settings::set('app_sms_tecent_secretKey', $request->input('app_sms_tecent_secretKey'), true);

        Settings::set('app_sms_smsbao_username', $request->input('app_sms_smsbao_username'), true);
        Settings::set('app_sms_smsbao_password', $request->input('app_sms_smsbao_password'), true);
        Settings::set('app_sms_smsbao_sign', $request->input('app_sms_smsbao_sign'), true);

        Settings::set('app_gaode_web_server_key', $request->input('app_gaode_web_server_key'), true);

        Settings::set('uni_push_1_appid', $request->input('uni_push_1_appid'), true);
        Settings::set('uni_push_1_appkey', $request->input('uni_push_1_appkey'), true);
        Settings::set('uni_push_1_master_secret', $request->input('uni_push_1_master_secret'), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
